
def genMatrix(rows,cols):
    matrix = [[0 for col in range(cols)] for row in range(rows)]  
    for i in range(rows):  
        for j in range(cols):  
            matrix[i][j]
    return matrix
def testSnake():
    number = 5
    matrix = genMatrix(number, number)
    i = 0
    j = 0
    total = matrix[i][j] = 1
    while(total < number * number):
        while(j + 1 < number and matrix[i][j + 1] == 0): 
            total += 1
            j += 1
            matrix[i][j] = total
        while(i + 1 < number and matrix[i + 1][j] == 0):
            total += 1
            i += 1
            matrix[i][j] = total
        while(j > 0 and matrix[i][j - 1] == 0): 
            total += 1
            j -= 1
            matrix[i][j] = total
        while(i + 1 > 0 and matrix[i - 1][j] == 0): 
            total += 1
            i -= 1
            matrix[i][j] = total
    for i in range(number):  
            for j in range(number):
                print ('\t%d ' % matrix[i][j], end='')
            print('\n')

testSnake()